package org.hhh.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hhh.basic.util.AjaxResult;
import org.hhh.basic.util.PageList;
import org.hhh.org.domain.Shop;
import org.hhh.org.query.ShopQuery;
import org.hhh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺管理接口数据对接json格式
 */

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;

    //新增或修改
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId()==null){
                shopService.add(shop);
            }else {
                shopService.upadate(shop);
            }
             return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙啊，请稍后");
        }
    }
    /**
     * 写接口三要素
     * 1.请求地址是什么
     * 2.请求的参数
     * 3.返回值是什么
     * @param id
     * @return
     */
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult removeId(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙,殴打程序猿");
        }
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchTree(@RequestBody List<Long> ids){
        try {
            shopService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙,殴打程序猿");
        }
    }

    //查询店铺一条数据
    @GetMapping("/{id}")
    public Shop getById(@PathVariable("id") Long id){
        return shopService.findById(id);
    }
    //查询全部数据
    @GetMapping("/list")
    public List<Shop> getAll(){
        return shopService.findAll();
    }

    //分页+高级查询
    @PostMapping("/page")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }


    /**
     * 店铺入住接口
     * @param shop  店铺入住信息
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlemen(@RequestBody Shop shop){
        try {
            shopService.settlemen(shop);
            return AjaxResult.me();
        } catch (RuntimeException e) {//抛出捕获自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage(e.getMessage());
        }catch (Exception e) {//系统级别错误
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙啊，请稍后");
        }
    }

}
